package com.atguigu.yygh.hosp.controller.api;

import com.alibaba.excel.util.StringUtils;
import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;

import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
   private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    /**
     * 删除排班接口
     * @param request
     * @return
     */
    @PostMapping("schedule/remove")
    public Result remove(HttpServletRequest request){
        Map<String, String[]> map = request.getParameterMap();
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(map);
        //校验
        String hoscode = (String)switchMap.get("hoscode");
        String hosScheduleId = (String)switchMap.get("hosScheduleId");

        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();


    }

    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){
        Map<String, String[]> map = request.getParameterMap();
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(map);


        String hoscode = (String)switchMap.get("hoscode");


        String depcode = (String)switchMap.get("depcode");


        int page = StringUtils.isEmpty(switchMap.get("page"))? 1:Integer.parseInt((String)switchMap.get("page"));
        int limit = StringUtils.isEmpty(switchMap.get("limit"))? 1:Integer.parseInt((String)switchMap.get("limit"));
        //签名校验
       ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

        Page<Schedule> pageModel= scheduleService.findPageSchedule(page,limit,scheduleQueryVo);


        return Result.ok(pageModel);
    }


    /**
     * 上传排班
     */
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, String[]> map = request.getParameterMap();
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(map);
        //签名校验

        scheduleService.save(switchMap);
        return Result.ok();
    }

    /**
     * 删除科室接口
     */


    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        Map<String, String[]> map = request.getParameterMap();
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(map);
        String hoscode = (String)switchMap.get("hoscode");
        String depcode = (String)switchMap.get("depcode");

        //签名校验
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }
    /**
     * 查询科室接口
     */
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request){
        Map<String, String[]> map = request.getParameterMap();
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(map);


       String hoscode = (String)switchMap.get("hoscode");
       int page = StringUtils.isEmpty(switchMap.get("page"))? 1:Integer.parseInt((String)switchMap.get("page"));
       int limit = StringUtils.isEmpty(switchMap.get("limit"))? 1:Integer.parseInt((String)switchMap.get("limit"));
       //签名校验
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

       Page<Department> pageModel= departmentService.findPageDepartment(page,limit,departmentQueryVo);


       return Result.ok(pageModel);
    }


    /**
     * 上传科室接口
     */

    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        Map<String, String[]> map = request.getParameterMap();
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(map);

        String hoscode=(String)switchMap.get("hoscode");
        //获取医院系统传递过来的签名
        String hospSign = (String) switchMap.get("sign");


        String signKey=hospitalSetService.getSignKey(hoscode);


        //数据库查出签名进行md5加 密
        String signKeyMd5 = MD5.encrypt(signKey);

        if(!signKeyMd5.equals(hospSign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        departmentService.save(switchMap);

        return Result.ok();


    }


    /**
     * 查询医院
     */
    @PostMapping("hospital/show")
    public Result gethospital(HttpServletRequest request){
        Map<String, String[]> map = request.getParameterMap();
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(map);

        String hoscode=(String)switchMap.get("hoscode");
        //获取医院系统传递过来的签名
        String hospSign = (String) switchMap.get("sign");


        String signKey=hospitalSetService.getSignKey(hoscode);


        //数据库查出签名进行md5加 密
        String signKeyMd5 = MD5.encrypt(signKey);

        if(!signKeyMd5.equals(hospSign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

    Hospital hospital= hospitalService.getByHosCode(hoscode);
        return Result.ok(hospital);
    }


    /**
     * 上传医院接口
     */
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request){
        Map<String, String[]> map = request.getParameterMap();
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(map);

        //获取医院系统传递过来的签名
        String hospSign = (String) switchMap.get("sign");

        //查看表中签名是否相同
        String hoscode = (String) switchMap.get("hoscode");

        String signKey=hospitalSetService.getSignKey(hoscode);


        //数据库查出签名进行md5加 密
        String signKeyMd5 = MD5.encrypt(signKey);

        if(!signKeyMd5.equals(hospSign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        String logo = (String) switchMap.get("logoData");
        logo = logo.replaceAll(" ", "+");
        switchMap.put("logoData",logo);
        hospitalService.save(switchMap);
        return Result.ok();

    }
}
